<?php
/**
 * Model for a Svn commit log.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Model for a Svn commit log.
 *
 * @package PhpCilux
 */
class Model_Log_Svn extends Model_Log_Abstract
{

    /**
     * Array with the commit log.
     *
     * @var array
     */
    protected $_commitLog = array();

    /**
     * Constructor.
     *
     * @param string $projectKey The project key.
     * @param string $buildKey   The build key.
     */
    public function __construct($projectKey, $buildKey)
    {
        parent::__construct($projectKey, $buildKey);

        $this->_logFilename = 'svnlog.xml';
        $this->_loadCommitLog();
    }

    /**
     * Check for existing the log-file of the log-type.
     *
     * @return boolean
     */
    public function exists()
    {
        return (parent::exists() && count($this->_commitLog));
    }

    /**
     * Get the status of the PHP Unit log.
     *
     * @return string
     */
    public function status()
    {
        return 'success';
    }

    /**
     * Formating the log.
     *
     * @param string $content The log content which will be styled.
     *
     * @return string
     */
    public function format($content)
    {
        return $content;
    }

    /**
     * Get the commit comment.
     *
     * @return string
     */
    public function getCommitComment()
    {
        return $this->_commitLog['comment'];
    }

    /**
     * Get the commit author.
     *
     * @return string
     */
    public function getCommitAuthor()
    {
        return $this->_commitLog['author'];
    }

    /**
     * Get the commit time.
     *
     * @return string
     */
    public function getCommitTime()
    {
        return $this->_commitLog['time'];
    }

    /**
     * Load the commit log.
     *
     * @return void
     */
    protected function _loadCommitLog()
    {
        $file = $this->_getLogFileName();

        if (is_file($file)) {
            $xml = simplexml_load_file($file);

            if (isset($xml->logentry->author)) {
                $this->_commitLog = array(
                    'author'  => (string) $xml->logentry->author,
                    'time'    => (string) $xml->logentry->date,
                    'comment' => (string) $xml->logentry->msg,
                );
            }
        }
    }

}